package com.training.iyogo.main;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.training.iyogo.domain.Card;
import com.training.iyogo.domain.Coach;
import com.training.iyogo.domain.Course;
import com.training.iyogo.domain.Schedule;
import com.training.iyogo.domain.VIP;
import com.training.iyogo.service.ICardService;
import com.training.iyogo.service.ICoachService;
import com.training.iyogo.service.ICourseService;
import com.training.iyogo.service.IScheduleService;
import com.training.iyogo.service.IVIPService;
import com.training.iyogo.service.impl.CardService;
import com.training.iyogo.service.impl.CoachService;
import com.training.iyogo.service.impl.CourseService;
import com.training.iyogo.service.impl.ScheduleService;
import com.training.iyogo.service.impl.VIPService;

/**
 * @author 志刚 办理会员和会员卡绑定 命令格式 添加会员:iyogo add-member [name] [address] [job]
 *         绑定会员卡：iyogo add-card [name] [cardtype] [times]
 *         备注：绑定会员卡中[times]选项只有cardtype=次卡时有用
 */
public class Main {

	private static IVIPService vipService;
	private static ICardService cardService;
	private static ICoachService coachService;
	private static ICourseService courseService;
	private static IScheduleService scheduleService;
	private static Logger logger = LoggerFactory.getLogger(Main.class);

	public static ApplicationContext appContext;

	/*
	 * 显示调用spring.xml以及初始化Service
	 */
	static {
		appContext = new ClassPathXmlApplicationContext("spring.xml");
		vipService = appContext.getBean(VIPService.class);
		cardService = appContext.getBean(CardService.class);
		coachService = appContext.getBean(CoachService.class);
		courseService = appContext.getBean(CourseService.class);
		scheduleService = appContext.getBean(ScheduleService.class);
	}

	@SuppressWarnings("resource")
	public static void main(String args[]) {
		Scanner in = new Scanner(System.in);
		while (true) {
			System.out.println("请输入请求：");
			String command = in.nextLine();// 读取命令行
			String[] cmds = command.split(" ");
			if (cmds.length > 2) {
				// 判断是新增会员还是绑定会员卡
				if (cmds[1].equals("add-member")) {
					VIP vip = new VIP();
					vip.setName(cmds[2]);
					vip.setAddress(cmds[3]);
					vip.setJob(cmds[4]);
					vipService.addVIP(vip);
					System.out.println("OK," + vip.getName() + "'s member id is " + vip.getVipId());
				} else if (cmds[1].equals("add-card")) {
					VIP vip = vipService.getByName(cmds[2]);// 通过会员名获取会员
					if (vip == null) {
						System.out.println("error!该会员不存在，请重新输入");
						logger.info("error!该会员不存在，请重新输入");
						continue;
					}
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					String startDate = sdf.format(new Date());
					Calendar calendar = Calendar.getInstance();
					int startYear = calendar.get(Calendar.YEAR);
					String month = String.format("%02d", calendar.get(Calendar.MONTH) + 1);
					String day = String.format("%02d", calendar.get(Calendar.DAY_OF_MONTH));
					int endYear = startYear + 1;
					Card card = new Card();
					card.setStartDate(startDate);
					card.setType(cmds[3]);
					if (card.getType().equals("次卡")) {
						endYear += 2;
						card.setRestTimes(Integer.parseInt(cmds[4]));
					}
					String endDate = String.valueOf(endYear) + "-" + month + "-" + day;
					card.setEndDate(endDate);
					card.setOwner(vip);
					cardService.addCard(card);
					Set<Card> cards = vip.getCards();
					vip.setCards(cards);
					if (card.getType().equals("年卡"))
						System.out.println("OK,A new 年卡  added to " + vip.getName() + "(" + vip.getVipId() + ")");
					else
						System.out.println("OK,A new 次卡  with " + cmds[4] + " times added to " + vip.getName());
				} else if (cmds[1].equals("add-coach")) {
					Coach coach = new Coach();
					coach.setName(cmds[2]);
					coach.setFamily(cmds[3]);
					coach.setAge(Integer.valueOf(cmds[4]));
					coach.setGoodAt(cmds[5]);
					coachService.addCoach(coach);
					System.out.println("OK,教练『" + coach.getName() + "』添加成功");
				} else if (cmds[1].equals("add-course")) {
					Course course = new Course();
					course.setCourseName(cmds[2]);
					courseService.addCourse(course);
					System.out.println("OK,课程『" + course.getCourseName() + "』添加成功");
				} else if (cmds[1].equals("schedule")) {
					Schedule schedule = new Schedule();
					schedule.setTime(cmds[2]);
					Course course = courseService.getByName(cmds[3]);
					Coach coach = coachService.getByName(cmds[4]);
					schedule.setCoach(coach);
					schedule.setCourse(course);
					scheduleService.addSchedule(schedule);
					System.out.println("OK,排课成功");
				} else if (cmds[1].equals("subscribe")) {
					Schedule schedule = scheduleService.getByTime(cmds[2]);
					VIP vip = vipService.getByName(cmds[3]);
					Set<Schedule> scheduleSet = vip.getSchedules();
					scheduleSet.add(schedule);
					vip.setSchedules(scheduleSet);
					//vipService.updateVIP(vip);
					String time = schedule.getTime();
					String formatTime = time.substring(0, 4) + "年" + time.substring(4, 6) + "月" + time.substring(6, 8)
							+ "日" + time.substring(9, 11) + "点" + time.substring(11, 13) + "分";
					System.out.println("OK," + vip.getName() + "预定成功，请于" + formatTime + "准时到达剑瑜伽馆，修炼"
							+ schedule.getCoach().getName() + "老师的" + schedule.getCourse().getCourseName());
				}else if (cmds[1].equals("cancel")){
					SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd-HHmmss");
					String now=sdf.format(new Date());
					boolean canCancel=true;
					if (cmds[2].substring(0, 8).equals(now.substring(0, 8))){
						now=now.substring(9,13);
						String orderTime=cmds[2].substring(9, 13);
						if (Integer.valueOf(orderTime)-Integer.valueOf(now)<=200)
							canCancel=false;
					}
					if (canCancel)
					{
						Schedule schedule=scheduleService.getByTime(cmds[2]);
						VIP vip=vipService.getByName(cmds[3]);
						Set<Schedule> scheduleSet=vip.getSchedules();
						scheduleSet.remove(schedule);
						vip.setSchedules(scheduleSet);
						//vipService.updateVIP(vip);
						System.out.println("取消成功");
					}else{
						System.out.println("sorry,取消只能在课程开始两个小时前");
					}
				}
				else {
					System.out.println("未识别请求，请重新输入");
					logger.info("未识别请求，请重新输入");
				}
			}
		}
	}
}
